package jdbclearn1;/*
@Date:2025/3/11
*/

import org.junit.Test;

import java.sql.*;

public class JdbcStudy {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        selectScore();
        insertScore();
        selectScore();
    }

    //数据查询
    public static void selectScore() throws ClassNotFoundException, SQLException {
        //注册操作数据的驱动 反射
        Class.forName("com.mysql.cj.jdbc.Driver");
        //使用api链接数据库
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=UTC", "root", "123456");
        System.out.println("conn = " + conn);
        //定义查询语句
        String sql = "select * from tbl_score";
        //获取查询对象
        Statement statement = conn.createStatement();
        System.out.println("statement = " + statement);
        //使用查询对象执行sql
        ResultSet resultSet = statement.executeQuery(sql);
        System.out.println("resultSet = " + resultSet);
        //解析数据
        while ((resultSet.next())) {
            int score_id = resultSet.getInt("score_id");
            System.out.println(score_id);
        }
    }

    //实现数据入库
    public static void insertScore() throws SQLException, ClassNotFoundException {
        //注册操作数据的驱动 反射
        Class.forName("com.mysql.cj.jdbc.Driver");
        //使用api链接数据库
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=UTC", "root", "123456");
        System.out.println("conn = " + conn);
        String sql = "insert into tbl_score values('7','6','6','6')";
        Statement statement = conn.createStatement();
        int i = statement.executeUpdate(sql);
        System.out.println("i = " + i);
    }
}
